<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/21 0021
 * Time: 20:37
 */

namespace com;

use think\Log;
use think\Session;

class XmlUtils
{

   static function xmlToArray($xml){
       $xmlobj=simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
       $arr = json_decode(json_encode($xmlobj),TRUE);
       return $arr;
   }

   static function arrayToXml($arr){
       if(!is_array($arr) || count($arr) <= 0){
           return false;
       }
       $xml = "<?xml version='1.0' encoding='UTF-8'?><xml>";
       foreach ($arr as $key=>$val){
           if (is_numeric($val)){
               $xml.="<".$key.">".$val."</".$key.">";
           }else{
               $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
               //$xml.="<".$key.">".$val."</".$key.">";
           }
       }
       $xml.="</xml>";
       return $xml;
   }



}